package wechat.rcv.controller

import com.shrcl.engineerprocess.domain.CorpWeChatUserDAO
import com.shrcl.engineerprocess.domain.EngineerWorkOrderCriteria
import edu.fudan.mylang.pf.IObjectFactory
import org.apache.log4j.Logger
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RestController
import java.text.SimpleDateFormat
import com.shrcl.engineerprocess.domain.EngineerWorkOrderStatus
import org.apache.commons.collections.IteratorUtils

@RestController
@Transactional
@ResponseBody
@RequestMapping("/myworkorder")
class GetMyWorkOrder {
	final static Logger logger = Logger.getLogger(typeof(GetMyWorkOrder).getName());

	@Autowired IObjectFactory entities;
	@Autowired CorpWeChatUserDAO corpWeChatUserDAO

	@RequestMapping(method = RequestMethod::POST,  produces = "application/json;charset=UTF-8" )
	def ArrayWapper process(String wechatid, Integer page, Integer pagesize) {
		logger.info('=========>GetMyWorkOrder.processs<============')
		logger.info('=========>GetMyWorkOrder.wechatid<============'+wechatid)
		logger.info('=========>GetMyWorkOrder.page<============'+page)
		logger.info('=========>GetMyWorkOrder.pagesize<============'+pagesize)
		try{
//			val engineer = corpWeChatUserDAO.findWeChatUser(wechatid)?.engineer
			val c = (entities.createQueryCriteria(typeof(EngineerWorkOrderCriteria))=>[
//				it.engineer = engineer
				it.^start = (page - 1) * pagesize
				it.^limit = pagesize 
			])
			val ret = new ArrayWapper
			val result = c.list.^map[o|
				new WXNotification=>[
					it.id = o.getId
					it.title = o.getProjectJob.getJobName
					it.date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(o.getStartDate)+' to ' + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(o.getEndDate)
					println('=========>GetMyWorkOrder.Status<============'+o.getStatus)
					if (o.getStatus == EngineerWorkOrderStatus.executing) {
						it.isFinished = false
					} else it.isFinished = true
				]
			]
			ret.result = IteratorUtils::toList(result.iterator);     
			ret.count = ret.result.size
			println("*******GetMyWorkOrder.result.size******"+ret.count)
			println("*******GetMyWorkOrder.response******")
			return ret
		} catch(Exception e){
			
		}
	}
}